package org.concepts.java.antlr.validator;

import static org.junit.Assert.assertTrue;

import java.util.LinkedHashMap;

import org.junit.Before;
import org.junit.Test;

public class TestValidator {

  private LinkedHashMap<String,String> data = null; 
  
  @Before
  public void beforeTest() {
    data = new LinkedHashMap<String,String>();
  }
  

  @Test
  public void testOR() {
    System.out.println("=============================================");
    assertTrue(Validator.validate(null, "true || false"));
  }
  
  
  @Test
  public void testLT() {
    System.out.println("=============================================");
    assertTrue(Validator.validate(null, " 3 < 5"));
  }
  
  @Test
  public void testPlus() {
    System.out.println("=============================================");
    assertTrue(Validator.validate(null, " 5 + 3 == 8"));
  }

  @Test
  public void testMultiply() {
    System.out.println("=============================================");
    assertTrue(Validator.validate(null, "1 + 5 * 3 == 16"));
  }

  @Test
  public void testSingleVariable() {
    System.out.println("=============================================");
    data.put("first", "35");
    assertTrue(Validator.validate(data, "$first == (30 + 5)"));
  }
  
  @Test
  public void testMultipleVariables() {
    System.out.println("=============================================");
    data.put("first", "35");
    data.put("second", "20");
    assertTrue(Validator.validate(data, "1 + 2 + $first > 40 || $second > 15"));
  }
  
  
  @Test
  public void testArithmeticOperations() {
    System.out.println("=============================================");
    data.put("first", "35");
    data.put("second", "20");
    assertTrue(Validator.validate(data, 
        " (7 + $first - 1) > 40 && $second < 15 * 2"));
  }
  
  
  @Test
  public void testFunctionCall() {
    System.out.println("=============================================");
    data.put("name", "foo");
    data.put("first", "35");
    data.put("second", "20");
    assertTrue(Validator.validate(data, 
        " (length($first) + 2) > 3 && $second < (15 * 2) &&" +
        " matches($name, foo)"));
  }
  
  @Test
  public void testVariableInVariable() {
    System.out.println("=============================================");
    data.put("first", "35");
    data.put("second", "20 + $first");
    data.put("third", "$second - $first");
    assertTrue(Validator.validate(data, 
        " $second > 25 && $third == 20"));
    
  }
  
  @Test
  public void testStringInExpression() {
    System.out.println("=============================================");
    assertTrue(Validator.validate(null,
        "matches(myjava,\"my.*java\")"));
    
  }
  
}
